//
//  garbageclientViewController.m
//  garbageclient
//
//  Created by Wang Wu on 11-8-10.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "garbageclientViewController.h"
#import "commondef.h"

#import "mainView.h"
#import "detailsView.h"
#import "commentView.h"
#import "searchView.h"
#import "infoView.h"
#import "infoEditView.h"
#import "setView.h"
#import "testView.h"
#import "UserSingleton.h"
#import "loginView.h"

@implementation garbageclientViewController

- (void)dealloc
{
    [mainBtn release];
    [searchBtn release];
    [personalBtn release];
    [setBtn release];
    
    [mainview release];
    [searchview release];
    [infoview release];
    [setview release];
    [loginview release];
    
    [super dealloc];
}

-(id)init
{
    self = [super init];
    if(self != nil)
    { 
        CGRect screenrc = [[UIScreen mainScreen] applicationFrame];
        screenrc.origin.y = 0;
        float btnheight = screenrc.size.height - BTN_HEIGHT;
        //creat four main view
        mainview = [[mainView alloc] initWithFrame:CGRectMake(0.0, 0.0, screenrc.size.width, btnheight)];
        mainview.delegate = self;
        
        setview = [[setView alloc] initWithFrame:CGRectMake(0.0, 0.0, screenrc.size.width, btnheight)];
        
        searchview = [[searchView alloc] initWithFrame:CGRectMake(0.0, 0.0, screenrc.size.width, btnheight)];
        searchview.delegate = self;
        
        infoview = [[infoView alloc] initWithFrame:CGRectMake(0.0, 0.0, screenrc.size.width, btnheight)];
        infoview.delegate = self;
        
        loginview = [[loginView alloc] initWithFrame:CGRectMake(0.0, 0.0, screenrc.size.width, btnheight)];
        loginview.delegate = self;
        
        currentview = mainview;
        
        mainBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        mainBtn.frame = CGRectMake(0.0, btnheight, BTN_WIDTH, BTN_HEIGHT);
        [mainBtn setTitle:@"主页" forState:UIControlStateNormal];
        [mainBtn setTitle:@"主页按下" forState:UIControlStateHighlighted];
        [mainBtn addTarget:self action:@selector(mainBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        
        searchBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        searchBtn.frame = CGRectMake(BTN_WIDTH, btnheight, BTN_WIDTH, BTN_HEIGHT);
        [searchBtn setTitle:@"搜索" forState:UIControlStateNormal];
        [searchBtn setTitle:@"搜索按下" forState:UIControlStateHighlighted];
        [searchBtn addTarget:self action:@selector(searchBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        
        personalBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        personalBtn.frame = CGRectMake(BTN_WIDTH * 2, btnheight, BTN_WIDTH, BTN_HEIGHT);
        [personalBtn setTitle:@"个人" forState:UIControlStateNormal];
        [personalBtn setTitle:@"个人按下" forState:UIControlStateHighlighted];
        [personalBtn addTarget:self action:@selector(personalBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        
        setBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        setBtn.frame = CGRectMake(BTN_WIDTH * 3, btnheight, BTN_WIDTH, BTN_HEIGHT);
        [setBtn setTitle:@"设置" forState:UIControlStateNormal];
        [setBtn setTitle:@"设置按下" forState:UIControlStateHighlighted];
        [setBtn addTarget:self action:@selector(setBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        
//        testview = [[testView alloc] initWithFrame:CGRectMake(0.0, 0.0, screenrc.size.width, btnheight)];
    }
    
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self.view addSubview:mainBtn];
    [self.view addSubview:searchBtn];
    [self.view addSubview:personalBtn];
    [self.view addSubview:setBtn];
   
    [self.view addSubview:mainview];
    
//    [self.view addSubview:testview];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark -viewdelegate
-(void)IsEnableBtn:(BOOL)benable
{
    [mainBtn setEnabled:benable];
    [searchBtn setEnabled:benable];
    [personalBtn setEnabled:benable];
    [setBtn setEnabled:benable];
}

-(void)TurnToInfo
{
    [self TurnView:INFO_VTP];
}

-(void)TurnToLogin
{
    [self TurnView:LOGIN_VTP];
}

#pragma mark - Turn View 

-(void)TurnView:(int)viewtype //iBackViewType default is NULL_DBTP
{
    [currentview removeFromSuperview];
//    [currentview release];
    
    switch (viewtype) 
    {
        case MAIN_VTP:
        {
            [self.view addSubview:mainview];
            
            currentview = mainview;
        }
            break;
            
        case SEARCH_VTP:
        {
            [self.view addSubview:searchview];
            
            currentview = searchview;
        }
            break;
            
        case INFO_VTP:
        {
            [self.view addSubview:infoview];
            
            currentview = infoview;
        }
            break;
            
        case SET_VTP:
        {
            [self.view addSubview:setview];     
            currentview = setview;
        }
            break;
            
        case LOGIN_VTP:
        {
            [self.view addSubview:loginview];
            currentview = loginview;
        }
            
        default:
            break;
    }
}

#pragma mark - Button click
-(void)mainBtnClick:(id)sender
{
    if(![currentview isKindOfClass:[mainView class]])
        [self TurnView:MAIN_VTP];
}

-(void)searchBtnClick:(id)sender
{
    if(![currentview isKindOfClass:[searchView class]])
        [self TurnView:SEARCH_VTP];
}

-(void)personalBtnClick:(id)sender
{
    if ([UserSingleton sharedSingleton].bhasUser) 
    {
        if(![currentview isKindOfClass:[infoView class]])
            [self TurnView:INFO_VTP];
    }
    else
    {
        if(![currentview isKindOfClass:[loginView class]])
            [self TurnView:LOGIN_VTP];
    }
}

-(void)setBtnClick:(id)sender
{
    if(![currentview isKindOfClass:[setView class]])
        [self TurnView:SET_VTP];
}

@end
